package org.zjx.service;

import org.zjx.common.bean.*;
import org.zjx.common.entity.Address;
import org.zjx.common.entity.Inventor;
import org.zjx.common.route.FlyingFlagsRoute;
import org.zjx.common.route.IRoute;
import org.zjx.common.service.LoadPlayerStateService;
import org.zjx.common.service.QueryService;
import org.zjx.db.*;
import org.zjx.route.RouteManager;
import org.zjx.server.annotation.RpcService;
import org.zjx.state.InventorState;
import org.zjx.state.PlayerState;
import org.zjx.state.StateManager;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

@RpcService(QueryService.class)
public class QueryServiceImpl implements QueryService {
    @Override
    public int haveItem(String playerId, String itemName, String attribute) {
        PlayerState playerState = StateManager.getInstance().getPlayerState(playerId);
        if (playerState == null) return 0;
        InventorState inventorState = playerState.getInventorState();
        if (inventorState == null) return 0;
        return inventorState.haveItem(itemName,attribute);
    }

    @Override
    public Npc queryNpc(String npcName, String screen, String room) {
        Screen screenObj = new ScreenDB().findByName(screen);
        Room roomObj = new RoomDB().findByName(room);
        return new NpcDB().findByNameAndScreenRoom(npcName,screenObj.getId(),roomObj.getId());
    }

    @Override
    public Npc queryItemNpc(String itemName) {
        return null;
    }

    @Override
    public Screen queryScreen(String screen) {
        return new ScreenDB().findByName(screen);
    }

    @Override
    public Room queryRoom(String screen, String roomName) {
        return new RoomDB().findByName(roomName);
    }

    @Override
    public Race queryRace(String race) {
        return new RaceDB().findByName(race);
    }

    @Override
    public Race quertRace(int raceId) {
        return new RaceDB().findById(raceId);
    }

    @Override
    public Sects querySect(String sectName) {
        return new SectsDB().findByDivision(sectName);
    }

    @Override
    public Sects querySect(int sectId) {
        return new SectsDB().findById(sectId);
    }

    @Override
    public Server queryServer(String serverName) {
        return new ServerDB().findByName(serverName);
    }

    @Override
    public Server queryServer(int serverId) {
        return new ServerDB().findById(serverId);
    }

    @Override
    public List<IRoute> queryRoute(Address startAddr, Address endAddr, boolean haveFlyingCharms) {
        RouteManager rm = new RouteManager();
        return rm.findPath(startAddr,endAddr,haveFlyingCharms,new ArrayList<>());
    }

    @Override
    public List<IRoute> queryRoute(String playerId, Address startAddr, Address endAddr) {

        List<FlyingFlagsRoute.FlyingFlagsPoint> flyingFlagsPoints = new ArrayList<>();
        List<Point> points = new ArrayList<>();
        points.add(new Point(100, 100));
        points.add(new Point(200, 200));
        points.add(new Point(300, 300));

        int haveFlyingCharms = haveItem(playerId,"飞行符",null);

        FlyingFlagsRoute.FlyingFlagsPoint flyingFlagsPoint = new FlyingFlagsRoute.FlyingFlagsPoint("长安城",points);
        flyingFlagsPoints.add(flyingFlagsPoint);

        RouteManager rm = new RouteManager();
        return rm.findPath(startAddr,endAddr,haveFlyingCharms > 0,flyingFlagsPoints);
    }
}
